//
//  NewSongsViewController.m
//  Mecca
//
//  Created by Nico Villanueva iOS Dev on 26/8/14.
//  Copyright (c) 2014 TapDash Heuristics. All rights reserved.
//

#import "NewSongsViewController.h"

#import "Constant.h"

#import "PostClass.h"

#import "JsonClass.h"

#import "ParserClass.h"

#import "NewSongsTableViewCell.h"

#import "StarRatingView.h"

#import "NewSongs.h"

#import "UIManager.h"

#import "Songs.h"

#import "Album.h"

#import "UserDetails.h"


@interface NewSongsViewController (){
    NSDictionary* json;
    
    NSDictionary *jsonDup;
}

@end

@implementation NewSongsViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [self.tableView setBackgroundColor:[UIColor colorWithRed:28/255.0 green:28/255.0 blue:28/255.0 alpha:1.0]];
    
    UserDetails *userDetails = [[ParserClass sharedItems] userDetails][0];
    
    self.receivedData = [NSMutableData data]; // Data
    NSURLRequest *request = [[PostClass sharedItems] getNewSong:[UIDevice currentDevice].identifierForVendor.UUIDString andUserID:[userDetails idUser]];
    _theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)btnBack:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}


#pragma mark - TableView Delegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    //#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return [[[ParserClass sharedItems] songDetails] count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Configure the cell...
    
    static NSString *cellIdentifier = @"Cell";
    
    NewSongsTableViewCell *cell = (NewSongsTableViewCell*)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    
    if (cell == nil) {
        
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"NewSongsTableViewCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }
    
    UIView *selectionColor = [[UIView alloc] init];
    selectionColor.backgroundColor = [UIColor colorWithRed:(38/255.0) green:(38/255.0) blue:(38/255.0) alpha:1];
    cell.selectedBackgroundView = selectionColor;

    
    
    
    Songs *newSongs = [[ParserClass sharedItems] songDetails][indexPath.row];
    
    cell.lblSongName.text = [newSongs songName];
    cell.lblDuration.text = [newSongs durations];
    
    
    int ratings = [[newSongs ratings] intValue];
    
    StarRatingView* starViewNoLabel = [[StarRatingView alloc]initWithFrame:CGRectMake(215, 19, 45, 8) andRating:ratings withLabel:NO animated:YES];
    
    [starViewNoLabel setUserInteractionEnabled:NO];
    [cell.customCell addSubview:starViewNoLabel];
    
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSLog(@"wew");
    
    Songs *newSongs = [[ParserClass sharedItems] songDetails][indexPath.row];
    
//    Songs *song = [[ParserClass sharedItems] songDetails][indexPath.row];
    
    [[ParserClass sharedItems] getUrlSong:[NSString stringWithFormat:@"http://192.185.93.102/~boomeran/backend-panel/%@", [newSongs songLocation]] andCount:indexPath.row andFileSize:[newSongs fileSize] andSongPreview:[NSString stringWithFormat:@"http://192.185.93.102/~boomeran/backend-panel/%@", [newSongs songPreview]]];
    
//    [[ParserClass sharedItems] getImgStr:[newSongs albumImg]];
    
    [[ParserClass sharedItems] getType:@"Songs"];
    
    [[ParserClass sharedItems] getImgStr:[[[jsonDup objectForKey:@"Songs"] objectAtIndex:indexPath.row] objectForKey:@"Album Image"]];
    
    [[ParserClass sharedItems] getArtistName:[[[jsonDup objectForKey:@"Songs"] objectAtIndex:indexPath.row] objectForKey:@"Artist Name"]];
    
    [[ParserClass sharedItems] getAlbumID:[[[jsonDup objectForKey:@"Songs"] objectAtIndex:indexPath.row] objectForKey:@"Album ID"]];
    
    [self.navigationController pushViewController:[[UIManager sharedItems] playSong] animated:YES];
    
    NSLog(@"Likes Data: %@",[newSongs likes]);
    
}

#pragma mark - URL Connection Delegate

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    [_receivedData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    NSLog(@"Error data = %@", error);
    
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Failed" message:@"Please check you internet connection." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    
    [alertView show];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
    
    
    if (connection == self.theConnection) {
        
        [_receivedData appendData:data];
        
        NSLog(@"data parse = %@", _receivedData);
        
    }
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
    
    if (connection == self.theConnection) {
        
        
        if (!(_receivedData == nil)) {
            NSError *error;
            
            NSDictionary *jsonData = [NSJSONSerialization JSONObjectWithData:_receivedData options:kNilOptions error:&error];
            
            NSLog(@"Received Data = %@", jsonData);
            
            [[ParserClass sharedItems] songDetails:jsonData];
            
            jsonDup = jsonData;
            
            [self.tableView reloadData];
            
            NSLog(@"New Songs = %@", jsonData);
            
            NSLog(@"Datas = %i",[[[ParserClass sharedItems] songDetails] count]);
            
            
            }
        
        
        NSLog(@"Finish");
    }
    
}

@end
